package com.oa.tools.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.dom4j.io.XMLWriter;

public class XmlUtil {
	
	public static String parseXml2StaffMap(String xmlStr, String tableid, XMLHandler xmlHandler) throws Exception {
		System.out.println(xmlStr);

		Document doc = DocumentHelper.parseText(xmlStr);
		Element root = doc.getRootElement();
		List<Node> nodes = root.selectNodes("/java/object/void");

		for (Node n : nodes) {
			Element ele = (Element) n;
			Node ele1 = ele.selectSingleNode("string");
			if (tableid.equals(ele1.getText().trim())) {
				List<Node> staff_nodes = ele.selectNodes("object/void/object");
				for (Node staff_node : staff_nodes) {
					xmlHandler.hanle(staff_node);
				}
				break;
			}
		}
		return doc.asXML();
	}
	
	public static Document readXml(String xmlFile) throws Exception {
		   // 创建saxReader对象  
        SAXReader reader = new SAXReader();  
        // 通过read方法读取一个文件 转换成Document对象  
        Document document = reader.read(new File(xmlFile));  
        SAXWriter writer = new SAXWriter();
        
        return document;
	}
	
	public static void writeXml(Document document,String file) throws Exception {
		
		XMLWriter writer=new XMLWriter(new FileOutputStream(file)/*, new OutputFormat(" ",true)*/);
		writer.write(document);
	}
	
	
	public static String parseXmlWithHandler(String xmlStr, String rootPath,XMLHandler xmlHandler) throws Exception {
		System.out.println(xmlStr);

		Document doc = DocumentHelper.parseText(xmlStr);
		Element root = doc.getRootElement();
		List<Node> nodes = root.selectNodes(rootPath);

		for (Node n : nodes) {
			xmlHandler.hanle(n);
		}
		return doc.asXML();
	}
	

	public static List<Map<String, String>> parseXml2StaffMap(String xmlStr, String tableid) throws Exception {
		System.out.println(xmlStr);
		List<Node> nodes = parseXmlStr(xmlStr, "/java/object/void");
		List<Map<String, String>> staffMapList = new ArrayList<>();

		for (Node n : nodes) {
			Element ele = (Element) n;
			Node ele1 = ele.selectSingleNode("string");
			if (tableid.equals(ele1.getText().trim())) {
				List<Node> staff_nodes = ele.selectNodes("object/void/object");
				for (Node staff_node : staff_nodes) {
					List<Node> staff_ele_nodes = staff_node.selectNodes("void");
					Map<String, String> staffMap = new HashMap<>();
					for (Node staff_ele_node : staff_ele_nodes) {
						String key = staff_ele_node.selectSingleNode("string[1]").getText();
						Node node = staff_ele_node.selectSingleNode("string[2]");
						String value = "";

						if (node == null) {
							node = staff_ele_node.selectSingleNode("object/long");
							if (node == null) {
								node = staff_ele_node.selectSingleNode("null");
								if (node == null) {
									node = staff_ele_node.selectSingleNode("object/void[2]/string[2]");
								}
							}
						}
						if (node != null) {
							value = node.getText();
						}

						staffMap.put(key, value);
					}
					staffMapList.add(staffMap);
				}

			}

		}

		System.out.println(staffMapList);
		return staffMapList;
	}

	public static Document getDocument(String xmlStr) throws Exception {
		Document Document = DocumentHelper.parseText(xmlStr);
		return Document;
	}

	public static List<Node> parseXmlStr(String xmlStr, String xpath) throws Exception {
		Document doc = DocumentHelper.parseText(xmlStr);
		Element root = doc.getRootElement();
		List<Node> nodes = root.selectNodes(xpath);

		return nodes;
	}

	public static abstract interface XMLHandler {
		public abstract void hanle(Node paramNode) throws Exception;
	}
	
	public static void main(String[] args) throws Exception {
		String src = "<dataList>\r\n" + 
				"			<data nodeId=\"N2\" name=\"杨佳伟\"/>\r\n" + 
				"			<data nodeId=\"N57\" name=\"赵萌;郑煜华;邹仁凯;聂庆磊\"/>\r\n" + 
				"			<data nodeId=\"N64\" name=\"乘用车-销售大区-东区-洛阳城市经理(杨佳伟)\"/>\r\n" + 
				"			<data nodeId=\"N65\" name=\"乘用车-销售大区-东区区域总监(聂庆磊)\"/>\r\n" + 
				"			<data nodeId=\"N48\" name=\"承贝;聂庆磊\"/>\r\n" + 
				"			<data nodeId=\"N49\" name=\"承贝;聂庆磊\"/>\r\n" + 
				"			<data nodeId=\"N50\" name=\"总经理(陈劲伟)\"/>\r\n" + 
				"			<data nodeId=\"N58\" name=\"刁鹏\"/>\r\n" + 
				"			<data nodeId=\"N66\" name=\"乘用车金融事业部负责人(岗位成员未设置)\"/>\r\n" + 
				"			<data nodeId=\"N54\" name=\"暂时无法计算处理人\"/>\r\n" + 
				"		</dataList>";
		
		Document document = XmlUtil.getDocument(src);
		Element root  = document.getRootElement();
		List<Element> children = root.elements();
		Map<String,String> nodeHandlerMap = new HashMap<>();
		for(Element child:children) {
			String key = child.attribute("nodeId").getText();
			String val = child.attribute("name").getText();
			nodeHandlerMap.put(key, val);
		}
		
		/*<dataList>
			<data nodeId="N2" name="杨佳伟"/>
			<data nodeId="N57" name="赵萌;郑煜华;邹仁凯;聂庆磊"/>
			<data nodeId="N64" name="乘用车-销售大区-东区-洛阳城市经理(杨佳伟)"/>
			<data nodeId="N65" name="乘用车-销售大区-东区区域总监(聂庆磊)"/>
			<data nodeId="N48" name="承贝;聂庆磊"/>
			<data nodeId="N49" name="承贝;聂庆磊"/>
			<data nodeId="N50" name="总经理(陈劲伟)"/>
			<data nodeId="N58" name="刁鹏"/>
			<data nodeId="N66" name="乘用车金融事业部负责人(岗位成员未设置)"/>
			<data nodeId="N54" name="暂时无法计算处理人"/>
		</dataList>*/
		
		System.out.println(nodeHandlerMap);
	}
}